package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.ListIterator;

/**
 * Created with IntelliJ IDEA.
 * User: dbelsey
 * Date: 30/01/13
 * Time: 22:01
 */
public abstract class LibraryCollection {
    protected ArrayList<LibraryItem> libraryContents;

    public LibraryCollection(){
        libraryContents = new ArrayList <LibraryItem>();
    }

    public void add(LibraryItem itemToAdd){
        libraryContents.add(itemToAdd);
    }

    public abstract void populateLibrary();

    public String getLibraryContentsToString(){
        ListIterator<LibraryItem> libraryIterator = libraryContents.listIterator();
        String libraryList = new String("List of library contents:");

        while(libraryIterator.hasNext()){
            libraryList = libraryList + "\n" + (libraryIterator.nextIndex()+1) +". "+ libraryIterator.next().getTitle();
        }
        return libraryList;
    }

    public boolean reserveBook(int bookNumberInList) throws InvalidLibraryItemNumberException {
        if(bookNumberInList > 0 && bookNumberInList <= libraryContents.size())
            return libraryContents.get(bookNumberInList - 1).reserve();
        else
            throw new InvalidLibraryItemNumberException();
    }

    public ArrayList <LibraryItem> getBookList(){
        return libraryContents;
    }
}
